package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.ContractCostingSheet2Adapter
import com.hinnotek.amoeba.databinding.ActivityContractCostSheet2Binding
import com.hinnotek.amoeba.ext.init
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.StatusBarUtil
import com.hinnotek.amoeba.utilities.ToastUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import java.util.*

class ContractCostingSheet2Activity : BaseActivity<ActivityContractCostSheet2Binding>() {

    private lateinit var mAdapter: ContractCostingSheet2Adapter
    private val stringList: MutableList<String> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("项目成本核算表")

        setContentView(R.layout.activity_contract_cost_sheet2)
    }

    override fun initVew() {
        StatusBarUtil.setStatusBarColor(this, R.color.colorbtns)
        bindingView.recyclerView.init()
        bindingView.recyclerView.adapter = ContractCostingSheet2Adapter().also {
            mAdapter = it
        }
        mAdapter.setOnItemClickListener { _: BaseQuickAdapter<*, *>?, _: View?, _: Int ->
            IntentUtils.startActivity(mContext, ProjectCostBreakdownActivity::class.java)
        }
    }

    override fun setData() {
        for (i in 0..9) {
            stringList.add("")
        }
        mAdapter.setList(stringList)

        //下拉刷新
        bindingView.refreshLayout.setOnRefreshListener { refreshLayout: RefreshLayout ->
            refreshLayout.layout.postDelayed(
                {
                    val status = refreshStatus
                    when (status) {
                        Status.SUCCESS -> {
                            mAdapter.setList(stringList)
                            bindingView.refreshLayout.resetNoMoreData() //setNoMoreData(false);
                            bindingView.llStateful.showContent()
                            bindingView.refreshLayout.setEnableLoadMore(true)
                        }
                        Status.EMPTY -> {
                            bindingView.llStateful.showEmpty()
                            bindingView.refreshLayout.setEnableLoadMore(false)
                        }
                        Status.ERROR -> showError()
                        Status.NO_NET -> showOffline()
                        else -> {
                        }
                    }
                    refreshLayout.finishRefresh()
                }, 0
            )
        }
        //上拉加载
        bindingView.refreshLayout.setOnLoadMoreListener { refreshLayout: RefreshLayout ->
            refreshLayout.layout.postDelayed(
                {
                    if (mAdapter.getItemCount() > 30) {
                        ToastUtils.showShort("数据全部加载完毕")
                        refreshLayout.finishLoadMoreWithNoMoreData() //将不会再次触发加载更多事件
                    } else {
                        mAdapter.addData(stringList)
                        refreshLayout.finishLoadMore()
                    }
                }, 0
            )
        }
        bindingView.refreshLayout.autoRefresh() //第一次进入触发自动刷新，演示效果
    }

    private fun showOffline() {
        bindingView.llStateful.showOffline(View.OnClickListener { v: View? -> bindingView.refreshLayout.autoRefresh() })
        bindingView.refreshLayout.setEnableLoadMore(false)
    }

    private fun showError() {
        bindingView.llStateful.showError(View.OnClickListener { v: View? -> bindingView.refreshLayout.autoRefresh() })
        bindingView.refreshLayout.setEnableLoadMore(false)
    }

    private enum class Status {
        SUCCESS, EMPTY, ERROR, NO_NET
    }

    private val refreshStatus: Status
        private get() = Status.SUCCESS
}